package com.symone.webcms.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.symone.webcms.mapper.DataExtMapper;
import com.symone.webcms.mapper.ModuleFieldMapper;
import com.symone.webcms.mapper.ModuleMapper;
import com.symone.webcms.model.Module;
import com.symone.webcms.model.ModuleField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class FrameworkUtil {

    @Autowired
    private ModuleMapper moduleMapper;
    @Autowired
    private ModuleFieldMapper fieldMapper;
    @Autowired
    private DataExtMapper dataExtMapper;

    public static FrameworkUtil frameworkUtil;

    protected static ConcurrentHashMap<String,String> config = new ConcurrentHashMap<String,String>();

    @PostConstruct
    public void init(){
        frameworkUtil = this;
        frameworkUtil.moduleMapper = this.moduleMapper;
        frameworkUtil.fieldMapper = this.fieldMapper;
        frameworkUtil.dataExtMapper = this.dataExtMapper;
    }

    public static List<ModuleField> getFieldsByModel(String moduleName){
        return frameworkUtil.fieldMapper.selectList(new EntityWrapper<ModuleField>().eq("module",moduleName).and().eq("deleted",0));
    }

    public static Module getModuleByCode(String code){
        return frameworkUtil.moduleMapper.selectOne(new Module().setCode(code));
    }

    public static Map<String,String> config(){
        return frameworkUtil.config;
    }

    public void config(Map<String,String> config){
        frameworkUtil.config.putAll(config);
    }

}
